<template>
  <div class="service-centers">
    <van-nav-bar
      :title="$t('repair.serviceCenters.title')"
      left-arrow
      @click-left="router.back()"
    />
    
    <!-- 搜索框 -->
    <div class="search-bar">
      <van-search
        v-model="searchKeyword"
        :placeholder="$t('repair.serviceCenters.searchPlaceholder')"
        @search="onSearch"
      />
    </div>
    
    <!-- 当前位置 -->
    <div class="location-bar">
      <van-icon name="location-o" />
      <span>{{ $t('repair.serviceCenters.currentLocation') }}: {{ currentLocation }}</span>
      <van-button size="small" type="primary" plain @click="relocate">
        {{ $t('repair.serviceCenters.relocate') }}
      </van-button>
    </div>
    
    <!-- 服务中心列表 -->
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model:loading="loading"
        :finished="finished"
        @load="onLoad"
      >
        <div class="center-list">
          <div
            v-for="center in centerList"
            :key="center.id"
            class="center-card"
            @click="selectCenter(center)"
          >
            <div class="center-header">
              <div class="center-name">
                <van-icon name="shop-o" />
                {{ center.name }}
              </div>
              <van-tag :type="center.businessStatus === 'OPEN' ? 'success' : 'danger'">
                {{ center.businessStatus === 'OPEN' ? $t('repair.serviceCenters.businessStatus') : $t('repair.serviceCenters.closed') }}
              </van-tag>
            </div>
            
            <div class="center-info">
              <div class="info-item">
                <van-icon name="location-o" />
                {{ center.address }}
              </div>
              <div class="info-item">
                <van-icon name="phone-o" />
                {{ center.phone }}
              </div>
              <div class="info-item">
                <van-icon name="clock-o" />
                {{ center.businessHours }}
              </div>
              <div class="info-item">
                <van-icon name="guide-o" />
                {{ $t('repair.serviceCenters.distance') }}: {{ center.distance }}km
              </div>
            </div>
            
            <div class="center-actions">
              <van-button size="small" icon="guide-o" @click.stop="viewMap(center)">
                {{ $t('repair.serviceCenters.viewMap') }}
              </van-button>
              <van-button size="small" type="primary" @click.stop="selectCenter(center)">
                {{ $t('repair.serviceCenters.appointment') }}
              </van-button>
            </div>
          </div>
        </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { showToast } from 'vant'
import { getServiceCenters } from '@/api/repair'
import { useRepairStore } from '@/stores/repair'

const router = useRouter()
const { t } = useI18n()
const repairStore = useRepairStore()

const searchKeyword = ref('')
const currentLocation = ref('北京市朝阳区')
const centerList = ref([])
const loading = ref(false)
const refreshing = ref(false)
const finished = ref(false)
const pageNum = ref(1)
const pageSize = 10

// 获取服务中心列表
const getCenterList = async (isRefresh = false) => {
  if (isRefresh) {
    pageNum.value = 1
    centerList.value = []
    finished.value = false
  }
  
  loading.value = true
  
  try {
    const params = {
      pageNum: pageNum.value,
      pageSize: pageSize
    }
    
    if (searchKeyword.value) {
      params.keyword = searchKeyword.value
    }
    
    const res = await getServiceCenters(params)
    
    if (isRefresh) {
      centerList.value = res.data.rows
    } else {
      centerList.value.push(...res.data.rows)
    }
    
    if (centerList.value.length >= res.data.total) {
      finished.value = true
    }
    
    pageNum.value++
  } catch (error) {
    showToast('获取服务中心失败')
  } finally {
    loading.value = false
    refreshing.value = false
  }
}

// 下拉刷新
const onRefresh = () => {
  getCenterList(true)
}

// 上拉加载
const onLoad = () => {
  getCenterList()
}

// 搜索
const onSearch = () => {
  getCenterList(true)
}

// 重新定位
const relocate = () => {
  showToast('定位中...')
  // TODO: 实现定位功能
}

// 查看地图
const viewMap = (center) => {
  showToast('打开地图')
  // TODO: 打开地图应用
}

// 选择服务中心
const selectCenter = (center) => {
  repairStore.updateFormData({
    serviceCenterId: center.id,
    serviceCenterName: center.name
  })
  router.back()
}

onMounted(() => {
  getCenterList(true)
})
</script>

<style scoped lang="scss">
.service-centers {
  min-height: 100vh;
  background: #f7f8fa;
}

.search-bar {
  background: #fff;
  padding: 8px 16px;
}

.location-bar {
  background: #fff;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: #646566;
  border-bottom: 1px solid #ebedf0;
  
  span {
    flex: 1;
  }
}

.center-list {
  padding: 16px;
}

.center-card {
  background: #fff;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  cursor: pointer;
  transition: all 0.3s;
  
  &:active {
    transform: scale(0.98);
  }
}

.center-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid #f7f8fa;
}

.center-name {
  font-size: 16px;
  font-weight: 500;
  color: #323233;
  display: flex;
  align-items: center;
  gap: 6px;
}

.center-info {
  margin-bottom: 12px;
}

.info-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: #646566;
  margin-bottom: 8px;
  
  &:last-child {
    margin-bottom: 0;
  }
  
  .van-icon {
    font-size: 14px;
    color: #969799;
  }
}

.center-actions {
  display: flex;
  gap: 12px;
  
  .van-button {
    flex: 1;
  }
}
</style>

